<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.util.List"%>
<%@ page import="com.google.appengine.api.datastore.*"%>
<%@ page import="com.google.appengine.api.memcache.*"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Praktikaportaal | Tööpakkumised</title>
</head>
<body>
	<%@ include file="header.jsp"%>
	<div id="content">
		<div class="content-container">
			<%
				DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
				Query query = new Query("Offer").addSort("company", Query.SortDirection.DESCENDING);
				List<Entity> offers = datastore.prepare(query).asList(FetchOptions.Builder.withDefaults());
				
				query = new Query("Company").addSort("name",  Query.SortDirection.DESCENDING);
				List<Entity> companies = datastore.prepare(query).asList(FetchOptions.Builder.withDefaults());

				if (offers.isEmpty()) {
			%>

			<p>Tööpakkumisi ei ole.</p>

			<%
				} else {
					for (Entity o : offers) {
						String position = (String) o.getProperty("position");
						String description = ((Text) o.getProperty("description")).getValue();
						Long companyId = (Long) o.getProperty("company");
						String link = "/company?id=" + companyId.toString();
			%>

			<h1><a href=<%= link %>><%= position %></a></h1>
			<p><%= description %></p>
			<hr>

			<%
					}
				}
			%>
		</div>
		<%
			if (isAdmin) {
		%>
		<div class="content-container">
			<h1>Lisa tööpakkumine</h1>
			<form action="/addoffer" method="post">
				<table>
					<tr>
						<td>
							<label for="company">Ettevõte: </label>
						</td>
						<td>
							<select id="company" name="company">
								<option value=""></option>
		<%
			for (Entity c : companies) {
				String companyName = (String) c.getProperty("name");
				String companyId = Long.toString(c.getKey().getId());
		%>
								<option value=<%= companyId %>><%= companyName %></option>
		<%
			}
		%>
							</select>
						</td>
					</tr>
					<tr>
						<td>
							<label for="position">Positsioon: </label>
						</td>
						<td>
							<input id="position" type="text" name="position"></input>
						</td>
					</tr>
					<tr>
						<td>
							<label for="description">Kirjeldus: </label>
						</td>
						<td>
							<textarea id="description" name="description"></textarea>
						</td>
					</tr>
				</table>
				<input type="submit" value="Salvesta"></input>
			</form>
		</div>
		<%
			}
		%>
	</div>
	<%@ include file="footer.jsp"%>
</body>
</html>